package com.shhic.hhplt.config;

import com.mongodb.client.MongoCollection;
import com.shhic.hhplt.server.model.MongoConstantName;
import org.bson.Document;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.mongodb.core.MongoTemplate;

@Configuration
public class MongoObject {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Bean
    @Primary
    public MongoCollection<Document> collection(){
        if(!mongoTemplate.collectionExists(MongoConstantName.colName)) {
            mongoTemplate.createCollection(MongoConstantName.colName);
        }
        return mongoTemplate.getCollection(MongoConstantName.colName);
    }


    @Bean("backup")
    public MongoCollection<Document> backupColl(){
        if(!mongoTemplate.collectionExists(MongoConstantName.backupName)) {
            mongoTemplate.createCollection(MongoConstantName.backupName);
        }
        return mongoTemplate.getCollection(MongoConstantName.backupName);
    }

}
